/**
 * 
 */
package com.cisetech.web.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cisetech.util.session.SystemRequestContext;
import com.cisetech.vo.bobocai.LUser;

/**
 * 自定义拦截器
 * @author zhaopeng
 * @date Apr 20, 2015
 */
public class MyInterceptor extends HandlerInterceptorAdapter {
	protected Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
	Date creat =null;
	Date end =null;
	/**
	 *执行后可释放资源
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		end = new Date();
		logger.info("结束时间====="+end);
	    long diff = end.getTime() - creat.getTime();
	    long days = diff / (1000 * 60 * 60 * 24);
		logger.info("耗时====="+days);
		super.afterCompletion(request, response, handler, ex);
	}

	/**
	 * 显示视图
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		creat = new Date();
		logger.info("开始时间====="+creat);
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * Controller之前执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String url = request.getServletPath();
		logger.info("url1====="+url);
		LUser user = (LUser) request.getSession().getAttribute("luser");
		SystemRequestContext.setHttpRequest(request);  
        SystemRequestContext.setSession(request.getSession());
		if(user != null){
			return super.preHandle(request, response, handler);
		}else{
			String name = request.getContextPath();
			System.out.println(name);
			response.sendRedirect(name+"/login");
			return false;
		}
	}
	
}
